/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.IllegalOrphanException;
import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.controllers.exceptions.PreexistingEntityException;
import com.applicatioN.entities.Certification;
import com.applicatioN.entities.CertificationPK;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Recevoir;
import java.util.ArrayList;
import java.util.Collection;
import com.applicatioN.entities.Exiger3;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class CertificationJpaController implements Serializable {

    public CertificationJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Certification certification) throws PreexistingEntityException, Exception {
        if (certification.getCertificationPK() == null) {
            certification.setCertificationPK(new CertificationPK());
        }
        if (certification.getRecevoirCollection() == null) {
            certification.setRecevoirCollection(new ArrayList<Recevoir>());
        }
        if (certification.getExiger3Collection() == null) {
            certification.setExiger3Collection(new ArrayList<Exiger3>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Recevoir> attachedRecevoirCollection = new ArrayList<Recevoir>();
            for (Recevoir recevoirCollectionRecevoirToAttach : certification.getRecevoirCollection()) {
                recevoirCollectionRecevoirToAttach = em.getReference(recevoirCollectionRecevoirToAttach.getClass(), recevoirCollectionRecevoirToAttach.getRecevoirPK());
                attachedRecevoirCollection.add(recevoirCollectionRecevoirToAttach);
            }
            certification.setRecevoirCollection(attachedRecevoirCollection);
            Collection<Exiger3> attachedExiger3Collection = new ArrayList<Exiger3>();
            for (Exiger3 exiger3CollectionExiger3ToAttach : certification.getExiger3Collection()) {
                exiger3CollectionExiger3ToAttach = em.getReference(exiger3CollectionExiger3ToAttach.getClass(), exiger3CollectionExiger3ToAttach.getExiger3PK());
                attachedExiger3Collection.add(exiger3CollectionExiger3ToAttach);
            }
            certification.setExiger3Collection(attachedExiger3Collection);
            em.persist(certification);
            for (Recevoir recevoirCollectionRecevoir : certification.getRecevoirCollection()) {
                Certification oldCertificationOfRecevoirCollectionRecevoir = recevoirCollectionRecevoir.getCertification();
                recevoirCollectionRecevoir.setCertification(certification);
                recevoirCollectionRecevoir = em.merge(recevoirCollectionRecevoir);
                if (oldCertificationOfRecevoirCollectionRecevoir != null) {
                    oldCertificationOfRecevoirCollectionRecevoir.getRecevoirCollection().remove(recevoirCollectionRecevoir);
                    oldCertificationOfRecevoirCollectionRecevoir = em.merge(oldCertificationOfRecevoirCollectionRecevoir);
                }
            }
            for (Exiger3 exiger3CollectionExiger3 : certification.getExiger3Collection()) {
                Certification oldCertificationOfExiger3CollectionExiger3 = exiger3CollectionExiger3.getCertification();
                exiger3CollectionExiger3.setCertification(certification);
                exiger3CollectionExiger3 = em.merge(exiger3CollectionExiger3);
                if (oldCertificationOfExiger3CollectionExiger3 != null) {
                    oldCertificationOfExiger3CollectionExiger3.getExiger3Collection().remove(exiger3CollectionExiger3);
                    oldCertificationOfExiger3CollectionExiger3 = em.merge(oldCertificationOfExiger3CollectionExiger3);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCertification(certification.getCertificationPK()) != null) {
                throw new PreexistingEntityException("Certification " + certification + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Certification certification) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Certification persistentCertification = em.find(Certification.class, certification.getCertificationPK());
            Collection<Recevoir> recevoirCollectionOld = persistentCertification.getRecevoirCollection();
            Collection<Recevoir> recevoirCollectionNew = certification.getRecevoirCollection();
            Collection<Exiger3> exiger3CollectionOld = persistentCertification.getExiger3Collection();
            Collection<Exiger3> exiger3CollectionNew = certification.getExiger3Collection();
            List<String> illegalOrphanMessages = null;
            for (Recevoir recevoirCollectionOldRecevoir : recevoirCollectionOld) {
                if (!recevoirCollectionNew.contains(recevoirCollectionOldRecevoir)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Recevoir " + recevoirCollectionOldRecevoir + " since its certification field is not nullable.");
                }
            }
            for (Exiger3 exiger3CollectionOldExiger3 : exiger3CollectionOld) {
                if (!exiger3CollectionNew.contains(exiger3CollectionOldExiger3)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Exiger3 " + exiger3CollectionOldExiger3 + " since its certification field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Recevoir> attachedRecevoirCollectionNew = new ArrayList<Recevoir>();
            for (Recevoir recevoirCollectionNewRecevoirToAttach : recevoirCollectionNew) {
                recevoirCollectionNewRecevoirToAttach = em.getReference(recevoirCollectionNewRecevoirToAttach.getClass(), recevoirCollectionNewRecevoirToAttach.getRecevoirPK());
                attachedRecevoirCollectionNew.add(recevoirCollectionNewRecevoirToAttach);
            }
            recevoirCollectionNew = attachedRecevoirCollectionNew;
            certification.setRecevoirCollection(recevoirCollectionNew);
            Collection<Exiger3> attachedExiger3CollectionNew = new ArrayList<Exiger3>();
            for (Exiger3 exiger3CollectionNewExiger3ToAttach : exiger3CollectionNew) {
                exiger3CollectionNewExiger3ToAttach = em.getReference(exiger3CollectionNewExiger3ToAttach.getClass(), exiger3CollectionNewExiger3ToAttach.getExiger3PK());
                attachedExiger3CollectionNew.add(exiger3CollectionNewExiger3ToAttach);
            }
            exiger3CollectionNew = attachedExiger3CollectionNew;
            certification.setExiger3Collection(exiger3CollectionNew);
            certification = em.merge(certification);
            for (Recevoir recevoirCollectionNewRecevoir : recevoirCollectionNew) {
                if (!recevoirCollectionOld.contains(recevoirCollectionNewRecevoir)) {
                    Certification oldCertificationOfRecevoirCollectionNewRecevoir = recevoirCollectionNewRecevoir.getCertification();
                    recevoirCollectionNewRecevoir.setCertification(certification);
                    recevoirCollectionNewRecevoir = em.merge(recevoirCollectionNewRecevoir);
                    if (oldCertificationOfRecevoirCollectionNewRecevoir != null && !oldCertificationOfRecevoirCollectionNewRecevoir.equals(certification)) {
                        oldCertificationOfRecevoirCollectionNewRecevoir.getRecevoirCollection().remove(recevoirCollectionNewRecevoir);
                        oldCertificationOfRecevoirCollectionNewRecevoir = em.merge(oldCertificationOfRecevoirCollectionNewRecevoir);
                    }
                }
            }
            for (Exiger3 exiger3CollectionNewExiger3 : exiger3CollectionNew) {
                if (!exiger3CollectionOld.contains(exiger3CollectionNewExiger3)) {
                    Certification oldCertificationOfExiger3CollectionNewExiger3 = exiger3CollectionNewExiger3.getCertification();
                    exiger3CollectionNewExiger3.setCertification(certification);
                    exiger3CollectionNewExiger3 = em.merge(exiger3CollectionNewExiger3);
                    if (oldCertificationOfExiger3CollectionNewExiger3 != null && !oldCertificationOfExiger3CollectionNewExiger3.equals(certification)) {
                        oldCertificationOfExiger3CollectionNewExiger3.getExiger3Collection().remove(exiger3CollectionNewExiger3);
                        oldCertificationOfExiger3CollectionNewExiger3 = em.merge(oldCertificationOfExiger3CollectionNewExiger3);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                CertificationPK id = certification.getCertificationPK();
                if (findCertification(id) == null) {
                    throw new NonexistentEntityException("The certification with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(CertificationPK id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Certification certification;
            try {
                certification = em.getReference(Certification.class, id);
                certification.getCertificationPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The certification with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Recevoir> recevoirCollectionOrphanCheck = certification.getRecevoirCollection();
            for (Recevoir recevoirCollectionOrphanCheckRecevoir : recevoirCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Certification (" + certification + ") cannot be destroyed since the Recevoir " + recevoirCollectionOrphanCheckRecevoir + " in its recevoirCollection field has a non-nullable certification field.");
            }
            Collection<Exiger3> exiger3CollectionOrphanCheck = certification.getExiger3Collection();
            for (Exiger3 exiger3CollectionOrphanCheckExiger3 : exiger3CollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Certification (" + certification + ") cannot be destroyed since the Exiger3 " + exiger3CollectionOrphanCheckExiger3 + " in its exiger3Collection field has a non-nullable certification field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(certification);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Certification> findCertificationEntities() {
        return findCertificationEntities(true, -1, -1);
    }

    public List<Certification> findCertificationEntities(int maxResults, int firstResult) {
        return findCertificationEntities(false, maxResults, firstResult);
    }

    private List<Certification> findCertificationEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Certification.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Certification findCertification(CertificationPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Certification.class, id);
        } finally {
            em.close();
        }
    }

    public int getCertificationCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Certification> rt = cq.from(Certification.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
